<?php
/*
* Author: Jason ZHENG <zmj522008@hotmil.com>
* Date  : 2018/05/25
* Class : Permission 权限管理
*/
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Permission extends Controller
{ 

	public function index()
	{   
		echo 'Permission Controller';
	}


     /**
     * 刘家腾
     * 2018-4-27
     * 添加权限
     */
    public function add_permissions()
    {
        //初始化参数
        $data['code']=666;
        $data['msg']='添加/修改 权限成功！';
        $data['data']='';
        $dates=input('post.');
        if(empty($dates)){
          $data['code']=30012;
          $data['msg']='没有添加/修改任何数据！';
        }
       
        if($data['code']==666)
        {
        	Db::table('permissions')->where(['user_id' => $dates['user_id']])->update(['state' => 1]);
	        foreach ($dates['module_ids'] as $moduleId) 
	        {
	        	Db::table('permissions')->insert(['user_id' =>$dates['user_id'], 'modules_all_id' => $moduleId, 'createtime' => date('Y:m:d H:i:s')]);
	        }
        }
        $data['data'] = Db::table('permissions')->where(['user_id' => $dates['user_id'], 'state' => 0])->select();
        return $data;
    }

    /**
     * 刘家腾
     * 2018-4-27
     * 模块列表
     */
    public function list_permissions()
    {
        //初始化参数
        $data['code']=666;
        $data['msg']='获取模块列表成功！';
        $data['data']='';
        $data['data']= Db::table('modules_all')
            ->whereOr('state',0)
            ->field('id module_id, state, type, modules_title, modules_name,  modules_funtion, grade, createtime')
            ->select();

       return $data;
    }

    /**
     * 刘家腾
     * 2018-4-27
     * 个人权限列表
     */
    public function permissions_info()
    {
      //初始化参数
      $data['code']=666;
      $data['msg']='获取个人权限成功！';
      $data['data']='';
      $dates=input('post.');
      $map['p.user_id']=$dates['user_id'];
      $map['p.state']=0;
      $data['data']=Db::table('permissions')
      				  ->alias("p")
				      ->join("modules_all m","p.modules_all_id=m.id","left")
				      ->where($map)
				      ->field(' p.user_id, p.modules_all_id module_id,p.state, m.type,m.modules_title, m.modules_name, m.modules_funtion,m.grade, p.createtime')
				      ->select();
      if($data['data']==''){
        $data['code']=30011;
        $data['msg']='获取个人权限失败！';
      }
      return $data;
    }

    /**
     * 刘家腾
     * 2018-4-26
     * 添加模板
     */
    public function add_module()
    {
      //初始化参数
      $data['code']=666;
      $data['msg']='添加模板成功！';
      $data['data']='';
      $map=[];
      $dates=input('post.');
      $user_info=$this->user;
      $issetdate = 'modules_name,modules_funtion,type,grade';
      $date=self::is_isset($issetdate,$dates);
      $map['system_id'] =$user_info['system_id'];
      $map['state'] =0;

      if($date['modules_name']){
        $map['modules_name'] =$date['modules_name'];
      }else{
        $data['code']=30003;
        $data['msg']='缺少必须参数！';
      }
      if($date['modules_funtion']){
        $map['modules_funtion'] =$date['modules_funtion'];
        $is_map['modules_funtion'] =$date['modules_funtion'];
      }else{
        $data['code']=30003;
        $data['msg']='缺少必须参数！';
      }
      if($date['type']){
        $map['type'] =$date['type'];
      }else{
        $data['code']=30003;
        $data['msg']='缺少必须参数！';
      }
      if($date['grade']){
        $map['grade'] =$date['grade'];
      }else{
        $data['code']=30003;
        $data['msg']='缺少必须参数！';
      }
      

      $is_modules_funtion=Db::table('modules_all')->where($is_map)->count();
      if($is_modules_funtion!=0){
        $data['code']==30012;
        $data['msg']='模板已经存在！';
      }elseif($data['code']==666){
        $map['business_id'] =$this->business_id;
        $map['store_id'] =$this->store_id;
        $data['data']=Db::table('modules_all')->insert($map);
      }

      return $data;
    }

    /**
     * 刘家腾
     * 2018-4-25
     * 过滤未定义变量
     */
    public function is_isset($value='',$date='')
    {
      $list=explode(',',$value);
      foreach ($list as $key => $v) {
        $data["$v"]=isset($date["$v"])?$date["$v"]:'0';
      }
      return $data;
    }


}